package com.ruoyi.client.controller;


import com.ruoyi.client.domain.vo.request.ClientEventApplyRequestVo;
import com.ruoyi.client.domain.vo.response.ClientEventDetailResponseVo;
import com.ruoyi.client.service.IEventApplyService;
import com.ruoyi.client.util.ResponseResult;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.EventDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 活动报名Controller
 * 
 * @author xukai
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/client/event")
public class ClientEventApplyController extends BaseController
{
    @Autowired
    private IEventApplyService eventApplyService;

    /**
     * 获取活动列表
     */
    @GetMapping("/list")
    public ResponseResult getEventList(){
        List<EventDetail> eventList = (List<EventDetail>) eventApplyService.getEventList();
        List<EventDetail> events = eventList.stream()
            .map(com.ruoyi.common.utils.EventStatusUtils::setEventStatus)
            .collect(Collectors.toList());
        return ResponseResult.success(events);
    }
    /**
     * 获取活动详情
     */
    @GetMapping("/detail")
    public ResponseResult getEventDetail(@RequestParam("eventId") Integer eventId, @RequestParam("userId") String userId){
        ClientEventDetailResponseVo eventDetail = eventApplyService.getEventDetail(eventId, userId);
        com.ruoyi.common.utils.EventStatusUtils.setEventStatus(eventDetail);
        return ResponseResult.success(eventDetail);
    }
    @PostMapping("/apply")
    private ResponseResult apply(@RequestBody ClientEventApplyRequestVo requestVo){
        int i = eventApplyService.apply(requestVo);
        if(i==-1){
            return ResponseResult.fail("已经报名过该活动");
        }else if(i==-2){
            return ResponseResult.fail("活动名额已满");
        }else if(i==-3){
            return ResponseResult.fail("活动报名未开始");
        }else if(i==-4){
            return ResponseResult.fail("活动报名已结束");
        }else {
            return ResponseResult.success("报名成功");
        }
    }
    /**
    *获取用户已报名且未开始的活动
     */
    @GetMapping("/unBeginEvent")
    private ResponseResult getUnBeginEvent (@RequestParam("userId") String userId){
        return ResponseResult.success(eventApplyService.getUnBeginEvent(userId));
    }

    /**
     * 获取未签到的活动
     */
    @GetMapping("/unSignInEvent")
    private ResponseResult getUnSignInEvent(@RequestParam("userId") String userId){
        return ResponseResult.success(eventApplyService.getUnSignInEvent(userId));
    }
    /**
     * 获取未签退的活动
     * */
    @GetMapping("/unSignOutEvent")
    private ResponseResult getUnSignOutEvent(@RequestParam("userId") String userId){
        return ResponseResult.success(eventApplyService.getUnSignOutEvent(userId));
    }

    /**
     * 获取已完成的活动
     * */
    @GetMapping("/finishedEvent")
    private ResponseResult getFinishedEvent(@RequestParam("userId") String userId){
        return ResponseResult.success(eventApplyService.getFinishedEvent(userId));
    }
    @GetMapping("/cancel")
    private ResponseResult cancelApply(@RequestParam("userId") String userId, @RequestParam("eventId") Integer eventId){
        int i = eventApplyService.cancelApply(userId, eventId);
        if(i!=0){
            return ResponseResult.success("取消报名成功");
        }else {
            return ResponseResult.fail("取消报名失败");
        }
    }

    @GetMapping("/signIn")
    private ResponseResult signIn(@RequestParam("userId") String userId, @RequestParam("eventId") Integer eventId,@RequestParam("code") String code){
        int i = eventApplyService.signIn(userId, eventId,code);
        if(i==1){
            return ResponseResult.success("签到成功");
        }else if(i==-1){
            return ResponseResult.fail("签到码不正确");
        }else if(i==-2){
            return ResponseResult.fail("不在签到时间内");

        }else {
            return ResponseResult.fail("签到失败");
        }

    }
    @GetMapping("/signOut")
    private ResponseResult signOut(@RequestParam("userId") String userId, @RequestParam("eventId") Integer eventId,@RequestParam("code") String code){
        int i = eventApplyService.signOut(userId, eventId,code);
        if(i==1){
            return ResponseResult.success("签退成功");
        }else if(i==-1){
            return ResponseResult.fail("签退码不正确");
        }else if(i==-2){
            return ResponseResult.fail("不在签退时间内");

        }else {
            return ResponseResult.fail("签退失败");
        }
    }

}
